/*
 *  MouseAndKeyboardForm.cs
 *  
 *  The form to demo advanced keyboard and mouse events.
 *  
 *  Copyright (c) 2009, Richard Bao Labs.
 *  
 *  The contents of this file are subject to the Mozilla Public License
 *  Version 1.1 (the "License"); you may not use this file except in
 *  compliance with the License. You may obtain a copy of the License at
 *  
 *  http://www.mozilla.org/MPL/
 *  
 *  Software distributed under the License is distributed on an "AS IS"
 *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 *  the License for the specific language governing rights and limitations
 *  under the License.
 *  
 *  The Original Code is RichControlLibrary.
 *  
 *  The Initial Developer of the Original Code is Richard Bao Labs.
 *  All Rights Reserved.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RichControlLibrary;
using System.Diagnostics;

namespace RichControlLibraryDemo
{
    public partial class MouseAndKeyboardForm : Form
    {
        private AdvancedMouseEventsMonitor _mouseMonitor = null;
        private AdvancedKeyboardEventsMonitor _keyboardMonitor = null;

        private Graphics g = null;
        private BufferedGraphics bg = null;

        public MouseAndKeyboardForm()
        {
            InitializeComponent();

            this.Font = SystemFonts.DialogFont;

            g = panelMouseCanvas.CreateGraphics();
            bg = BufferedGraphicsManager.Current.Allocate(g, panelMouseCanvas.ClientRectangle);
            
            _mouseMonitor = new AdvancedMouseEventsMonitor(this.panelMouseCanvas);
            _mouseMonitor.MouseClick += delegate { ShowMouseEventName("MouseClick"); };
            _mouseMonitor.MouseDown += delegate { ShowMouseEventName("MouseDown"); };
            _mouseMonitor.MouseDragging += delegate { ShowMouseEventName("MouseDragging"); };
            _mouseMonitor.MouseDraggingFinished += delegate { ShowMouseEventName("MouseDraggingFinished"); };
            _mouseMonitor.MouseLeftButtonClick += delegate { ShowMouseEventName("MouseLeftButtonClick"); };
            _mouseMonitor.MouseLeftButtonDown += delegate { ShowMouseEventName("MouseLeftButtonDown"); };
            _mouseMonitor.MouseLeftButtonUp += delegate { ShowMouseEventName("MouseLeftButtonUp"); };
            _mouseMonitor.MouseMove += new AdvancedMouseEventHandler(_mouseMonitor_MouseMove);
            _mouseMonitor.MouseMoveWithoutDragging += delegate { ShowMouseEventName("MouseMoveWithoutDragging"); };
            _mouseMonitor.MouseRightButtonClick += delegate { ShowMouseEventName("MouseRightButtonClick"); };
            _mouseMonitor.MouseRightButtonDown += delegate { ShowMouseEventName("MouseRightButtonDown"); };
            _mouseMonitor.MouseRightButtonUp += delegate { ShowMouseEventName("MouseRightButtonUp"); };
            _mouseMonitor.MouseStartDragging += delegate { ShowMouseEventName("MouseStartDragging"); };
            _mouseMonitor.MouseUp += delegate { ShowMouseEventName("MouseUp"); };

            _keyboardMonitor = new AdvancedKeyboardEventsMonitor(this);
            _keyboardMonitor.KeyDown += delegate { UpdatePressedKeys(); };

            UpdateMouseInfo();
            UpdatePressedKeys();
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            bg.Dispose();
            g.Dispose();
        }

        private void _mouseMonitor_MouseMove(object sender, AdvancedMouseEventArgs e)
        {
            ShowMouseEventName("MouseMove");
            UpdateMouseInfo();
        }

        private void ShowMouseEventName(string eventName)
        {
            Trace.WriteLine("MouseEvent: " + eventName);
        }

        private void UpdateMouseInfo()
        {
            if (_mouseMonitor.IsDragging)
            {
                bg.Graphics.Clear(panelMouseCanvas.BackColor);
                bg.Graphics.DrawRectangle(Pens.White, _mouseMonitor.DraggedBounds);
                bg.Render();

                lblIsDragging.Text = "Yes";
                lblDragAnchor.Text =
                    "(" +
                    _mouseMonitor.DraggingAnchor.X.ToString() + ", " +
                    _mouseMonitor.DraggingAnchor.Y.ToString() + ")";
                lblDraggedSize.Text =
                    "(" +
                    _mouseMonitor.DraggedSize.Width.ToString() + ", " +
                    _mouseMonitor.DraggedSize.Height.ToString() + ")"; 
            }
            else
            {
                lblIsDragging.Text = "No";
                lblDragAnchor.Text = "";
                lblDraggedSize.Text = "";
            }

            if (panelMouseCanvas.Bounds.Contains(panelMouseCanvas.PointToClient(Control.MousePosition)))
            {
                lblLocation.Text =
                    "(" +
                    _mouseMonitor.Location.X.ToString() + ", " +
                    _mouseMonitor.Location.Y.ToString() + ")"; 
            }
            else
            {
                lblLocation.Text = "";
            }
        }

        private void UpdatePressedKeys()
        {
            lstPressedKeys.Items.Clear();

            foreach (Keys key in _keyboardMonitor.PressedKeys)
            {
                lstPressedKeys.Items.Add(key.ToString());
            }

            lblShift.Text = (_keyboardMonitor.IsShiftPressed ? "Pressed" : "");
            lblCtrl.Text = (_keyboardMonitor.IsCtrlPressed ? "Pressed" : "");
            lblAlt.Text = (_keyboardMonitor.IsAltPressed ? "Pressed" : "");
        }
    }
}